package com.rnkrsoft.suyan.config.server.services.impl;

import com.rnkrsoft.framework.orm.Pagination;
import com.rnkrsoft.suyan.config.internal.enums.OperateObjectEnum;
import com.rnkrsoft.suyan.config.internal.enums.OperateTypeEnum;
import com.rnkrsoft.suyan.config.protocol.v2.ConfigResponseCode;
import com.rnkrsoft.suyan.config.server.config.bo.*;
import com.rnkrsoft.suyan.config.server.config.dao.*;
import com.rnkrsoft.suyan.config.server.config.entity.*;
import com.rnkrsoft.suyan.config.server.services.logger.services.LoggerService;
import com.rnkrsoft.suyan.config.server.services.version.domains.*;
import com.rnkrsoft.suyan.config.server.services.version.services.VersionService;
import com.rnkrsoft.time.DateStyle;
import com.rnkrsoft.utils.DateUtils;
import com.rnkrsoft.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by woate on 2019/6/25.
 */
@Service
public class VersionServiceImpl implements VersionService {
    @Autowired
    ProjectInfoDAO projectDAO;
    @Autowired
    VersionInfoDAO versionInfoDAO;
    @Autowired
    ProjectDependencyDAO projectDependencyDAO;
    @Autowired
    LoggerService loggerService;

    @Override
    public ToCreateVersionResponse toCreate(ToCreateVersionRequest request) {
        ToCreateVersionResponse response = new ToCreateVersionResponse();
        ProjectInfoEntity projectInfoEntity = projectDAO.selectByPrimaryKey(request.getProjectId());
        if (projectInfoEntity == null) {
            response.updateResult(ConfigResponseCode.NO_DATA);
            return response;
        }
        List<VersionInfoEntity> versionInfoEntities = versionInfoDAO.selectAnd(VersionInfoEntity.builder().projectId(projectInfoEntity.getProjectId()).build());
        response.setProjectId(request.getProjectId());
        for (VersionInfoEntity versionInfoEntity : versionInfoEntities){
            response.getVersions().add(ToCreateVersionRecord.builder()
                    .versionId(versionInfoEntity.getVersionId())
                    .version(versionInfoEntity.getVersion())
                    .build()
            );
        }
        return response;
    }

    @Override
    public CreateVersionResponse create(CreateVersionRequest request) {
        CreateVersionResponse response = new CreateVersionResponse();
        ProjectInfoEntity projectInfoEntity = projectDAO.selectByPrimaryKey(request.getProjectId());
        if (projectInfoEntity == null) {
            response.updateResult(ConfigResponseCode.FAIL);
            return response;
        }
        //根据项目以来的项目信息创建对应的版本号
        List<ProjectDependencyEntity> dependencyEntities = projectDependencyDAO.selectAnd(ProjectDependencyEntity.builder().projectId(request.getProjectId()).build());
        for (ProjectDependencyEntity dependencyEntity : dependencyEntities) {
            VersionInfoEntity dependencyVersionInfoEntity = VersionInfoEntity.builder()
                    .projectId(dependencyEntity.getParentProjectId())
                    .version(request.getVersion())
                    .build();
            //如果依赖项目没有对应版本号，则创建
            if (versionInfoDAO.selectAnd(dependencyVersionInfoEntity).isEmpty()) {
                versionInfoDAO.insertSelective(dependencyVersionInfoEntity);
            }
        }
        //创建当前项目的对应版本
        VersionInfoEntity versionInfoEntity = VersionInfoEntity.builder()
                .projectId(request.getProjectId())
                .version(request.getVersion())
                .build();
        versionInfoDAO.insertSelective(versionInfoEntity);
        response.setVersionId(versionInfoEntity.getVersionId());
        return response;
    }

    @Override
    public DeleteVersionResponse delete(DeleteVersionRequest request) {
        DeleteVersionResponse response = new DeleteVersionResponse();

        loggerService.writeLog(OperateObjectEnum.VERSION, request.getVersionId(), OperateTypeEnum.UPDATE, request.getLoginUserId(), request.getLoginUserName());

        versionInfoDAO.deleteByPrimaryKey(request.getVersionId());
        return response;
    }

    @Override
    public QueryVersionResponse query(QueryVersionRequest request) {
        QueryVersionResponse response = new QueryVersionResponse();
        Pagination<QueryVersionInfoBO> pagination = new Pagination<QueryVersionInfoBO>(request.getPagination().getPageSize(), request.getPagination().getPageNo());
        if (StringUtils.isNotBlank(request.getProjectId())) {
            pagination.getParams().put("projectId", request.getProjectId());
        }
        if (StringUtils.isNotBlank(request.getVersion())) {
            pagination.getParams().put("version", request.getVersion());
        }
        versionInfoDAO.selectPageVersion(pagination);
        response.getPagination().setPageSize(request.getPagination().getPageSize());
        response.getPagination().setPageNo(request.getPagination().getPageNo());
        response.getPagination().setTotal(pagination.getTotal());
        response.getPagination().setPageNum(pagination.getPageNum());
        for (QueryVersionInfoBO record : pagination.getRecords()) {
            response.getRecords().add(QueryVersionRecord.builder()
                    .versionId(record.getVersionId())
                    .groupId(record.getGroupId())
                    .artifactId(record.getArtifactId())
                    .version(record.getVersion())
                    .createDate(DateUtils.toString(record.getCreateDate(), DateStyle.SECOND_FORMAT2))
                    .lastUpdateDate(DateUtils.toString(record.getLastUpdateDate(), DateStyle.SECOND_FORMAT2))
                    .build());
        }
        return response;
    }
}
